package com.costeaalex.datedrivenfilecopy.model;

/**
 * @author Alex Costea
 * 
 * Eraser model encapsulates the recursive business logic to erase previous files using an IEraser.
 * 
 */

import java.util.ArrayList;
import java.util.Observable;

import com.costeaalex.datedrivenfilecopy.model.erasers.EraseFile;
import com.costeaalex.datedrivenfilecopy.model.interfaces.IEraser;

public class EraserModel extends Observable implements Runnable
	{
	
	private IEraser e;
	private EraseFile eF;
	
	public EraserModel(IEraser e, EraseFile eF) {
		this.e=e;
		this.eF=eF;
	}
	
	public void erase(EraseFile eF)
		{
		ArrayList<EraseFile> files = e.getFileList(eF);
		EraseFile tmp;
		
		for(int i=0; i<files.size(); i++)
			{
			tmp=files.get(i);
			if(tmp.isDirectory())
				erase(tmp);
			e.delete(tmp);
			}
		}

	@Override
	public void run() {
		e.init();
		erase(eF);
		e.destroy();
		setChanged();
		notifyObservers("Done erasing");		
	}
}
